package ca.ubc.icapture.genapha.actions;

import ca.ubc.icapture.genapha.beans.PhenotypeStatus;
import ca.ubc.icapture.genapha.beans.StatusBean;
import ca.ubc.icapture.genapha.forms.DBStatusForm;
import icapture.SQLMgr;
import icapture.genapha.GenotypeToolsManager;
import icapture.beans.DB.Cohort;
import icapture.beans.DB.Status;
import icapture.beans.DB.StatusType;
import icapture.beans.DB.Phenotypes;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: btripp
 * Date: Sep 13, 2006
 * Time: 9:38:59 AM
 * To change this template use File | Settings | File Templates.
 */
public class DBStatusAction extends Action
{

    @Override
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
    {

        DBStatusForm statusForm = (DBStatusForm) form;

        ArrayList<PhenotypeStatus> cohortSummaryList = new ArrayList<PhenotypeStatus>();
        statusForm.setCohortSummary(cohortSummaryList);

        //load summary statistics into form bean for display
        StatusType completeTrioStatusType = SQLMgr.getStatusType(0); //get complete trios
        StatusType oneParentStatusType = SQLMgr.getStatusType(1); //get complete trios
        StatusType unafectedCompleteTrioStatusType = SQLMgr.getStatusType(4); //get complete trios

        ArrayList<Cohort> cohortList = SQLMgr.getCohorts();
        Iterator<Cohort> iter = cohortList.iterator();
        while (iter.hasNext())
        {
            Cohort cohort = iter.next();

            PhenotypeStatus status = new PhenotypeStatus();
            status.setCohort(cohort);
            status.setComplete(SQLMgr.getStatus(completeTrioStatusType, cohort).getValue());
            status.setOneParent(SQLMgr.getStatus(oneParentStatusType, cohort).getValue());

            cohortSummaryList.add(status);
        }

        ArrayList<StatusBean> statusBeanList = new ArrayList<StatusBean>();
        statusForm.setStatusBeans(statusBeanList);

        //load per phenotype statistics
        ArrayList<Phenotypes> phenotypeList = SQLMgr.getPhenotypes();
        Iterator<Phenotypes> pIter = phenotypeList.iterator();
        while (pIter.hasNext())
        {
            Phenotypes phenotype = pIter.next();
            StatusBean statusBean = new StatusBean();

            ArrayList<PhenotypeStatus> phenotypeStatusList = new ArrayList<PhenotypeStatus>();
            statusBean.setPhenotypeStatusList(phenotypeStatusList);
            statusBean.setPhenotype(phenotype);
            iter = cohortList.iterator();
            while (iter.hasNext())
            {
                Cohort cohort = iter.next();

                PhenotypeStatus phenotypeStatus = new PhenotypeStatus();
                phenotypeStatus.setCohort(cohort);
                phenotypeStatus.setComplete(SQLMgr.getStatus(completeTrioStatusType, cohort, phenotype).getValue());
                phenotypeStatus.setOneParent(SQLMgr.getStatus(oneParentStatusType, cohort, phenotype).getValue());
                phenotypeStatus.setUnaffectedComplete(SQLMgr.getStatus(unafectedCompleteTrioStatusType, cohort, phenotype).getValue());

                phenotypeStatusList.add(phenotypeStatus);
            }
            statusBeanList.add(statusBean);
        }

        if (statusForm.getSubmitType() != null && statusForm.getSubmitType().equalsIgnoreCase("regen"))
        {
            //recompute all the summary statistics
            //launch thread to process request
            new Thread()
            {

                @Override
                public void run()
                {
                    GenotypeToolsManager gtm = new GenotypeToolsManager();
                    gtm.updateAffectedStats();
                }
            }.start();
        }

        return mapping.findForward("display");
    }
}
